<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:db="livcos.org/ns/db"
	xmlns:ddl="livcos.org/ns/db/ddl"
	xmlns:q="livcos.org/ns/query"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:v="livcos.org/ns/view"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="db ddl q f v xs">
	
	<xsl:import href="../../query/view.xsl"/>
	
	<xsl:template match="ddl:lookup">
		<v:chapter id="{@id}">
			<v:title>Lookup Values</v:title>
			<xsl:apply-templates select="* except ddl:*"/>
		</v:chapter>
	</xsl:template>
	
	<xsl:template match="ddl:lookup" mode="lookup">
		<xsl:param name="tableName"/>
		<xsl:param name="fieldName"/>
		<xsl:variable name="data" as="element()">
			<xsl:apply-templates select="if (@query) then f:entity(@query) else q:sql[1]" mode="data"/>
		</xsl:variable>
		<xsl:variable name="res" as="element()">
			<xsl:element name="{name($data)}">
				<xsl:copy-of select="$data/@*"/>
				<xsl:apply-templates select="$data/db:results" mode="#current">
					<xsl:with-param name="tableName" select="$tableName"/>
					<xsl:with-param name="fieldName" select="$fieldName"/>
					<xsl:with-param name="tableNameCol" select="@table-name-col"/>
					<xsl:with-param name="fieldNameCol" select="@field-name-col"/>
				</xsl:apply-templates>
			</xsl:element>
		</xsl:variable>
		<xsl:if test="$res/*/db:*">
			<xsl:variable name="viz">
				<table xmlns="livcos.org/ns/doc/visualization">
					<title>
						<xsl:text>Lookup </xsl:text>
						<a href="{../@name}.html#{@id}" xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="../@name"/></a>
					</title>
					<rows>
						<xsl:apply-templates select="$res/*/ddl:meta/ddl:column" mode="query_viz"/>
					</rows>
					<data content-type="text/xml">
						<xsl:element name="{name($res)}" namespace="">
							<xsl:copy-of select="$res/@*"/>
							<xsl:element name="{name($res/*)}" namespace="livcos.org/ns/db">
								<xsl:copy-of select="$res/*/@*"/>
								<xsl:copy-of select="$res/*/db:*"/>
							</xsl:element>
						</xsl:element>
					</data>
				</table>
			</xsl:variable>
			<xsl:apply-templates select="$viz/*"/>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="db:results" mode="lookup">
		<xsl:param name="tableName"/>
		<xsl:param name="fieldName"/>
		<xsl:param name="tableNameCol"/>
		<xsl:param name="fieldNameCol"/>
		<xsl:copy>
			<xsl:copy-of select="@*"/>
			<xsl:apply-templates select="ddl:meta" mode="#current">
				<xsl:with-param name="filterCols" select="($tableNameCol, $fieldNameCol)"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="db:*[db:*[position() = $fieldNameCol] = $fieldName]" mode="#current">
				<xsl:with-param name="filterCols" select="($tableNameCol, $fieldNameCol)"/>
			</xsl:apply-templates>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="ddl:meta" mode="lookup">
		<xsl:param name="filterCols" as="xs:integer*"/>
		<xsl:copy>
			<xsl:copy-of select="@*"/>
			<xsl:copy-of select="ddl:*[not(index-of($filterCols, position()))]"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="db:*" mode="lookup">
		<xsl:param name="filterCols" as="xs:integer*"/>
		<xsl:copy>
			<xsl:copy-of select="@*"/>
			<xsl:copy-of select="db:*[not(index-of($filterCols, position()))]"/>
		</xsl:copy>
	</xsl:template>
	
</xsl:stylesheet>
